/*
 * @(#)MeetingMessageInfo.java  
 * Copyright (c) 2006 DCIVision Ltd All rights reserved. 
 * This software is the confidential and proprietary information of DCIVision Ltd ("Confidential Information"). You *
 * shall not disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.alert.core;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.dcivision.alert.bean.UpdateAlert;
import com.dcivision.alert.bean.UpdateAlertSystemLog;
import com.dcivision.alert.dao.UpdateAlertSystemLogDAObject;
import com.dcivision.calendar.bean.CalendarRecord;
import com.dcivision.calendar.dao.CalendarRecordDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.Utility;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.dao.UserRecordDAObject;

/**
 * <p>
 * Class Name: MeetingMessageInfo.java
 * </p>
 * 
 * @author Greatwall shao
 * @company DCIVision Limited
 * @creation date 2006-09-24
 */
public class MeetingMessageInfo extends AbstractMessageInfo {

  Integer          objectid;

  CalendarRecord   calendarRecord;

  public MeetingMessageInfo(Integer objectid) {
    this.objectid = objectid;
  }

  public void setOtherInfo()  throws ApplicationException{
    CalendarRecordDAObject calendarRecordDAO = new CalendarRecordDAObject(sessionContainer, dbConn);
    calendarRecord = (CalendarRecord) calendarRecordDAO.getOriCalendarByID(objectid);
    this.messageInfoBean.setVenue(calendarRecord.getVenue());
    this.messageInfoBean.setDetail( calendarRecord.getDetail());
    
    setFromAndToTime();
    setReplyAttributes();
  }

  private void setFromAndToTime() throws ApplicationException, NumberFormatException {
      
    String formatPattern = "yyyy-MM-dd hh:mm a";
    if(GlobalConstant.TRUE.equals(calendarRecord.getIsWholeDay()))
    {
      formatPattern= SystemParameterFactory.getSystemParameter(
                  SystemParameterConstant.DATETIME_FORMAT).substring(0, 10);//remove hh:mm..
    }
    else if(!Utility.isEmpty(SystemParameterFactory.getSystemParameter(
                              SystemParameterConstant.DATETIME_FORMAT)))
    {
      formatPattern= SystemParameterFactory.getSystemParameter(
          SystemParameterConstant.DATETIME_FORMAT).trim()+" a";//add AM/PM
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat(formatPattern,sessionContainer.getSessionLocale());
   if(!Utility.isEmpty(calendarRecord.getDatetime())){
    this.messageInfoBean.setFrom(dateFormat.format(calendarRecord.getDatetime()));
   }
   if(!Utility.isEmpty(calendarRecord.getEndtime())){
    this.messageInfoBean.setTo(dateFormat.format(calendarRecord.getEndtime()));
   }
  }

  private void setReplyAttributes() throws ApplicationException {
    UserRecordDAObject userRecordDAO = new UserRecordDAObject(sessionContainer, dbConn);
    UpdateAlertSystemLogDAObject alertSystemLogDAO = new UpdateAlertSystemLogDAObject(
        sessionContainer, dbConn);

    List validUpdateAlertID = new ArrayList(1);
    validUpdateAlertID.add(this.alertSystemLog.getUpdateAlertID());

    List systemLogList = alertSystemLogDAO.getSystemLogListForInvite(validUpdateAlertID,UpdateAlert.INVITE_ACTION);
    List replyOnAcceptAry = new ArrayList();
    List replyOnTentativeAry = new ArrayList();
    List replyOnRejectAry = new ArrayList();
    if (systemLogList != null) {
      for (int i = 0; i < systemLogList.size(); i++) {
        UpdateAlertSystemLog alertSystemLog = (UpdateAlertSystemLog) systemLogList.get(i);
        replyOnAcceptAry = alertSystemLogDAO.getReplyActionByUpdateAlertID(alertSystemLog,
            UpdateAlert.ACCEPT_ON_REPLY);
        replyOnTentativeAry = alertSystemLogDAO.getReplyActionByUpdateAlertID(
            alertSystemLog, UpdateAlert.TENTATIVE_ON_REPLY);
        replyOnRejectAry = alertSystemLogDAO.getReplyActionByUpdateAlertID(alertSystemLog,
            UpdateAlert.REJECT_ON_REPLY);
      }
    }

    this.messageInfoBean.setAccept(getReplyStr(userRecordDAO, replyOnAcceptAry));
    this.messageInfoBean.setTentative(getReplyStr(userRecordDAO, replyOnTentativeAry));
    this.messageInfoBean.setReject(getReplyStr(userRecordDAO, replyOnRejectAry));
  }

  private String getReplyStr(UserRecordDAObject userRecordDAO, List replyList)
      throws ApplicationException {
    if ((replyList != null) && (replyList.size() > 0)) {
      String reply = "";
      UserRecord userRecord;
      String name = "";
      UpdateAlertSystemLog updateAlertSystemLog;
      for (int j = 0; j < replyList.size(); j++) {
        updateAlertSystemLog = (UpdateAlertSystemLog) replyList.get(j);
        userRecord = (UserRecord) userRecordDAO.getObjectByID(updateAlertSystemLog
            .getActionUserID());
        if (userRecord != null) {
          name = userRecord.getFullName() == null ? "" : userRecord.getFullName();
        }
        reply += name;
        reply += " , ";
      }
      reply = cutEndComma(reply);
      return reply;
    }
    return "";
  }
}
